import { Component, OnInit } from '@angular/core';
import { NavParams, NavController } from '@ionic/angular';
import { Helper } from 'src/app/providers/Helper';
import { ShopService } from 'src/app/services/shop.service';
import { ToastService } from 'src/app/providers/toast/toast.service';
import * as constants from 'src/app/providers/Constants'
import { Utils } from 'src/app/providers/Utils';
@Component({
  selector: 'app-goods-select',
  templateUrl: './goods-select.component.html',
  styleUrls: ['./goods-select.component.scss'],
})
export class GoodsSelectComponent implements OnInit {
  btnType:number=1;
  num:number=1;
  btnStr:string='Add to Cart'
  procPriceId:string=''
  procList:any=[];
  procInfo:any={};
  procListColor: any = [];
  constructor(private nav:NavParams,private helper:Helper,private navController:NavController,private toast:ToastService,private shop:ShopService) {
    this.btnType=this.nav.data.btnType;
    if (this.nav.data.procList) {
      this.procList=this.nav.data.procList;
      this.procInfo=this.nav.data.shopDetail;
      this.procPriceId=this.nav.data.procPriceId;
      if (this.nav.data.dataColor) {
        this.procListColor=this.nav.data.dataColor;
      }
    } 
    console.log('procInfo',this.procInfo);
    
    if (this.btnType==1) {
      this.btnStr='Add to Cart'
    } else {
      this.btnStr='Checkout'
    }
   }
   funOnCilic(type){
      if (type==1) {//减法
        if (this.num>1) {
          this.num--;
        }
      } else {
        this.num++;
      }
   }
  ngOnInit() {

  }
  onClickPay(){
    let color=''
    for (let index = 0; index <  this.procListColor.length; index++) {
      console.log('this.procListColor',this.procListColor);
      const element = this.procListColor[index];
      if(element.isShow){
        color=element.color;
      }
    }
    let isOk=false;
    for (let index = 0; index < this.procList.length; index++) {
      const element = this.procList[index];
      if (element.isShow==true) {
        isOk=true;
      }
    }
    if (!isOk) {
      this.toast.show('Please select specifications');
      return;
    }
    if (this.btnType==1) { //Add to Cart
        this.helper.toast('Add to Cart');
        this.shop.addShoppingCar({ procId: this.procInfo.procId,
          num:this.num,
          color:color,
         //  size:this.procInfo.procColorNum,
          size: this.procInfo.size,
          merchCode: this.procInfo.merchCode,
          }).subscribe(res => {
         // 保存成功返沪上一页，否则弹出提示框
         if (res.retCode == '000000') {
           this.toast.show('successful');
         } else if (constants.errCode[res.retCode]) {
           if (constants.errCode[res.retCode] != '0') {
             this.toast.show(constants.errCode[res.retCode]);
           }
         } else if (res.retMsg) {
           this.toast.show(res.retMsg);
         } else {
           this.toast.show("exception service routine");
         }
       });
         this.close();
    } else { // 购买
      this.closeModel();
      this.procInfo.num=this.num;
      // this.procInfo.procPriceId= this.procInfo.procCashPriceId;
      this.navController.navigateForward('order-create',{
        queryParams:{
          procPriceId:this.procPriceId,
          goodsInfo:JSON.stringify(this.procInfo)
        }
      });
    }
  }
  onItemColor(item) {
    console.log(item);
    for (const key in this.procListColor) {
      if (this.procListColor.hasOwnProperty(key)) {
        const element = this.procListColor[key];
        if (item.color == element.color) {
          console.log(element);
          this.procListColor[key].isShow = true;
        } else {
          this.procListColor[key].isShow = false;
        }

      }
    }
  }
  getColor(){
    this.shop.getProcStandardsByColor({
      procStandards: this.procInfo.size,
      merchCode: this.procInfo.merchCode,
      procName: this.procInfo.procName,
    }).subscribe(res => {
      console.log(res);
      if (res.retCode == '000000') {
        for (let index = 0; index < res.array.length; index++) {
          if (index==0) {
            res.array[index].isShow=true;
          }else{
            res.array[index].isShow=false; 
          }
        }
        this.procListColor=res.array
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    })
  }
  solaImage1(img){
    return Utils.imageUrl(this.shop.getImageUrl(),img);
  }

  onItem(item) {
    console.log(item);
    for (const key in this.procList) {
      if (this.procList.hasOwnProperty(key)) {
        const element = this.procList[key];
        console.log(element);
        if (element.procId == item.procId) {
          this.procList[key].isShow = true;
          // this.procName = element.procName + '  ' + element.procRamSize + 'GB+' + element.procRomSize + 'GB'
          // }else{
          //   this.procName = element.procName ;
          // }
          // this.procCashPrice = this.AmountOfConversion(element.procCashPrice)
          // this.procImg = this.shop.getImageUrl() + element.procImg.split(',')[0];
          this.procInfo=element;
          this.getColor();
        } else {
          this.procList[key].isShow = false;
        }
    }
  }
  }
  AmountOfConversion(num: number) {
    return num / 100;
  }
  close(){ 
      this.closeModel();
  }
  closeModel() {
    this.nav.data.modal.dismiss({
      'result': '消失的时候返回的内容',
    });
  }
}
